/**
 * Copyright (c) 2012 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.android.wordbattle;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import chabernac.android.tools.Translator;
import chabernac.android.wordbattle.db.AsyncDatabaseLoaderDecorator;
import chabernac.android.wordbattle.db.CommentWordFilter;
import chabernac.android.wordbattle.db.CompositeWordFilter;
import chabernac.android.wordbattle.db.DataBaseLoader;
import chabernac.android.wordbattle.db.InMemoryStringDB;
import chabernac.android.wordbattle.db.InvalidWordFilter;
import chabernac.android.wordbattle.db.LengthWordFilter;
import chabernac.android.wordbattle.db.SQLiteStringDB;
import chabernac.android.wordbattle.db.URLWordLoader;
import chabernac.android.wordbattle.db.iStringDatabase;
import chabernac.android.wordbattle.db.iWordFilter;
import chabernac.android.wordbattle.db.iWordLoader;
import chabernac.android.wordbattle.model.WordRasterFactory;

public class WordRasterContext {

  private static class INSTANCE_HOLDER{
    public static WordRasterContext Instance = new WordRasterContext();
  }

  private WordRasterGameMediator myMediator = null;
  private iStringDatabase myStringDatabase;
  private iWordLoader myWordLoader = new URLWordLoader( "http://wordbattle.googlecode.com/files" );
  private iWordFilter myWordFilter = new CompositeWordFilter()
  .addFilter( new CommentWordFilter() )
  .addFilter( new LengthWordFilter( 3, 4 ))
  .addFilter( new InvalidWordFilter() );
  private WordRasterFactory myWordRasterFactory = null;

  private AsyncDatabaseLoaderDecorator myDataBaseLoader  = null;

  private Context myContext = null;

  private List<iWordRasterContextListener> myListeners = new ArrayList<iWordRasterContextListener>();

  public WordRasterGameMediator getMediator() {
    return myMediator;
  }

  public void setMediator( WordRasterGameMediator aMediator ) {
    myMediator = aMediator;
    for(iWordRasterContextListener theListener : myListeners) theListener.wordRasterGameLoaded(aMediator);
  }

  public void addListener(iWordRasterContextListener aListener){
    myListeners.add(aListener);
  }

  public void removeListener(iWordRasterContextListener aListener){
    myListeners.remove(aListener);
  }

  public static void init(Context aContext){
    getInstance().initContext(aContext);
    Translator.initialize(aContext);
  }

  public void initContext(Context aContext){
    if(myContext == null){
      myContext = aContext;

      //use SQLite DB
          myStringDatabase = new SQLiteStringDB( aContext );

      //use in memory tree db
      //    myStringDatabase = new InMemoryTreeDB();

//      myStringDatabase = new InMemoryStringDB();
      myDataBaseLoader = new AsyncDatabaseLoaderDecorator( new DataBaseLoader( myWordLoader, myWordFilter, myStringDatabase, -1 ) );
      myWordRasterFactory = new WordRasterFactory( myStringDatabase );
    }
  }

  public static WordRasterContext getInstance(){
    return INSTANCE_HOLDER.Instance;
  }

  public AsyncDatabaseLoaderDecorator getDatabaseLoaderDecorator(){
    return myDataBaseLoader;
  }

  public iStringDatabase getStringDatabase(){
    return myStringDatabase;
  }

  public Context getContext(){
    return myContext;
  }

  public WordRasterFactory getWordRasterFactory() {
    return myWordRasterFactory;
  }

  public void setWordRasterFactory( WordRasterFactory aWordRasterFactory ) {
    myWordRasterFactory = aWordRasterFactory;
  }

}
